package com.example.device.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.device.dto.DeviceUpdateRequest;
import com.example.device.dto.UpdateCheckResult;
import com.example.device.entity.DeviceConfig;
import com.example.device.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/update/check")
    public ResponseEntity<UpdateCheckResult> checkBeforeUpdate(@RequestBody DeviceUpdateRequest request) {
        return ResponseEntity.ok(deviceService.checkBeforeUpdate(request));
    }

    @PostMapping("/update/confirm")
    public ResponseEntity<String> confirmUpdate(@RequestBody DeviceUpdateRequest request) {
        deviceService.performUpdate(request);
        return ResponseEntity.ok("更新成功");
    }

    @GetMapping("/page")
    public ResponseEntity<Page<DeviceConfig>> getDevicePage(
            @RequestParam("page") long page,
            @RequestParam("size") long size
    ) {
        Page<DeviceConfig> devicePage = deviceService.getDevicePage(page, size);
        return ResponseEntity.ok(devicePage);
    }
}